package com.example.x3day04.base;

import android.content.Context;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.ArrayMap;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;


import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

//T 指定的是子条目的数据
public abstract class BaseRlvAdapter<T> extends RecyclerView.Adapter<BaseRlvAdapter.ViewHolder>{
    public Context context;
    private ArrayList<T> list;

    public BaseRlvAdapter(Context context, ArrayList<T> list) {
        this.context = context;
        this.list = list;
    }

    @NonNull
    @Override
    public BaseRlvAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = LayoutInflater.from(context).inflate(getLayout(), null);
        return new ViewHolder(inflate);
    }

    protected abstract int getLayout();

    @Override
    public void onBindViewHolder(@NonNull BaseRlvAdapter.ViewHolder holder, int position) {
        T t = list.get(position);
        bindData(holder,t);
    }

    protected abstract void bindData(ViewHolder holder, T t);

    @Override
    public int getItemCount() {
        return list.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        SparseArray<View> mMap = new SparseArray<>();

        public ViewHolder(View itemView) {
            super(itemView);
        }

        public View findView(@IdRes int id){
            View view = mMap.get(id);
            if (view == null){
                view = itemView.findViewById(id);
                mMap.put(id,view);
            }
            return view;
        }
        public void setText(@IdRes int id,String text){
            try {
                TextView view = (TextView) findView(id);
                view.setText(text);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}
